package com.qk.management.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.dto.user.UserDTO;
import com.qk.entity.User;
import com.qk.management.aop.anno.LogAnno;
import com.qk.management.service.UserService;
import com.qk.vo.user.UserVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 *
 * @author lyl
 * @created 2025/9/30 15:22
 * @description
 *
 */
@Slf4j
@RestController
@SuppressWarnings("all")
@RequestMapping("/users")
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping
    public Result list(UserDTO userDTO){
        log.info("分页查询用户,参数:用户名称={},用户状态={},手机={},部门ID={},当前页码={},每页大小={}", userDTO.getName(), userDTO.getStatus(), userDTO.getPhone(), userDTO.getDeptId(), userDTO.getPage(), userDTO.getPageSize());
        PageResult<UserVO> pageResult = userService.list(userDTO);
        return Result.success(pageResult);
    }

    @LogAnno
    @PostMapping
    public Result addUser(@RequestBody User user){
        log.info("添加用户:user={}", user.toString());
        userService.addUser(user);
        return Result.success();
    }

    @GetMapping("dept/{deptId}")
    public Result listByDept(@PathVariable Integer deptId){
        log.info("查询部门下的用户,部门ID={}", deptId);
        List<User> list = userService.listByDept(deptId);
        return Result.success(list);
    }

    @GetMapping("role/{roleLabel}")
    public Result listByRole(@PathVariable String roleLabel){
        log.info("查询角色下的用户,角色={}", roleLabel);
        List<User> list = userService.listByRole(roleLabel);
        return Result.success(list);
    }

    @GetMapping("/list")
    public Result getAll(){
        log.info("查询所有用户");
        List<User> list = userService.getAll();
        return Result.success(list);
    }

    @LogAnno
    @PutMapping
    public Result updateUser(@RequestBody User user){
        log.info("修改用户:user={}", user.toString());
        userService.updateUser(user);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result getUserById(@PathVariable Integer id){
        log.info("查询用户,用户ID={}", id);
        User user = userService.getUserById(id);
        return Result.success(user);
    }

    @LogAnno
    @DeleteMapping("/{ids}")
    public Result deleteUsers(@PathVariable("ids") List<Integer> ids){
        log.info("删除用户,用户ID={}", ids);
        userService.deleteUsers(ids);
        return Result.success();
    }
}
